package com.foodost.idiomer;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import android.content.Context;
import android.content.SharedPreferences;

public class Utils {
	
    
    //获取整个成语的汉语拼音
    public static String getPinyin(String hanzi){
        
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); 

     // UPPERCASE：大写  (ZHONG)
     // LOWERCASE：小写  (zhong)
     format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

     // WITHOUT_TONE：无音标  (zhong)
     // WITH_TONE_NUMBER：1-4数字表示英标  (zhong4)
     // WITH_TONE_MARK：直接用音标符（必须WITH_U_UNICODE否则异常）  (zhòng)
     format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);

     // WITH_V：用v表示ü  (nv)
     // WITH_U_AND_COLON：用"u:"表示ü  (nu:)
     // WITH_U_UNICODE：直接用ü (nü)
     format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
     int length = hanzi.toCharArray().length;
     String[][] pinyin = new String[length][10];
     StringBuilder sb = new StringBuilder();
     try {
         for(int i = 0; i< length; i++){
             pinyin[i] = PinyinHelper.toHanyuPinyinStringArray(hanzi.charAt(i), format);
             sb.append(pinyin[i][0] + " ");
         }
        
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     
     return sb.toString();
        
    }
    
    public static void idiomInitFilish(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putBoolean(Constants.IS_INITDATA, true);
        editor.apply();
    }
    
    public static void explanInitFilish(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putBoolean(Constants.IS_INITEDEXPLAN, true);
        editor.apply();
    }
    
    public static int getOrmGrage(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        int ormGrage = sf.getInt(Constants.ORM_GRAGE, 1);
        return ormGrage;
    }
    
    public static void ormGrage(Context context,int resId){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        SharedPreferences.Editor editor = sf.edit();
        switch(resId){
        case R.id.grage1:
            editor.putInt(Constants.ORM_GRAGE, 1);
            break;
        case R.id.grage2:
            editor.putInt(Constants.ORM_GRAGE,2);
            break;
        case R.id.grage3:
            editor.putInt(Constants.ORM_GRAGE, 3);
            break;
            default:
            editor.putInt(Constants.ORM_GRAGE, 1);
        }
        
        editor.apply();
    }
    
    public static void setPlaysSetting(Context context,int resId){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        SharedPreferences.Editor editor = sf.edit();
        switch(resId){
        case R.id.playnomal:
            editor.putInt(Constants.PLAYS_SETTING, 1);
            break;
        case R.id.playone:
            editor.putInt(Constants.PLAYS_SETTING,2);
            break;
            default:
            editor.putInt(Constants.PLAYS_SETTING, 1);
        }
        
        editor.apply();
    }
    
    public static int getPlaysSetting(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        int playsetting = sf.getInt(Constants.PLAYS_SETTING, 1);
        return playsetting;
    }
    
    
    public static boolean getInitShortCut(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        boolean shortcutInited = sf.getBoolean(Constants.SHORTCUT_INITED, false);
        return shortcutInited;
    }
    
    public static void setInitShortCut(Context context,boolean inited){
        SharedPreferences sf = context.getSharedPreferences(Constants.DB_SHARED, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putBoolean(Constants.SHORTCUT_INITED, inited);
        editor.apply();
    }

}
